package com.yjm.test.Spring5.ch4.propertyEditor;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.regex.Pattern;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.context.support.GenericXmlApplicationContext;

public class PropertyEditorBean {
	private byte[] bytes;
	private Character character;
	private Class cls;
	private Boolean trueorFalse;
	private List<String> stringList;
	private Date date;
	private Float floatValue;
	private File file;
	private InputStream stream;
	private Locale locale;
	private Pattern pattern;
	private Properties properties;
	private String trimString;
	private URL url;
	
	
	public void setBytes(byte[] bytes) {
		System.out.println("setting bytes: " + Arrays.toString(bytes));
		this.bytes = bytes;
	}


	public void setCharacter(Character character) {
		System.out.println("setting character: " + character);
		this.character = character;
	}


	public void setCls(Class cls) {
		System.out.println("setting class: " + cls.getName());
		this.cls = cls;
	}


	public void setTrueorFalse(Boolean trueorFalse) {
		System.out.println("setting boolean: " + trueorFalse);
		this.trueorFalse = trueorFalse;
	}


	public void setStringList(List<String> stringList) {
		System.out.println("setting list:" + stringList.size());
		this.stringList = stringList;
		for (String s:stringList) {
			System.out.println("\tString member:" + s);
		}
	}


	public void setDate(Date date) {
		System.out.println("setting date: " + date);
		this.date = date;
	}


	public void setFloatValue(Float floatValue) {
		System.out.println("setting float value: " + floatValue);
		this.floatValue = floatValue;
	}


	public void setFile(File file) {
		System.out.println("setting file: " + file.getName());
		this.file = file;
	}


	public void setStream(InputStream stream) {
		System.out.println("setting stream:" + stream);
		this.stream = stream;
	}


	public void setLocale(Locale locale) {
		System.out.println("setting locale:" + locale.getDisplayName());
		this.locale = locale;
	}


	public void setPattern(Pattern pattern) {
		System.out.println("setting pattern: " + pattern);
		this.pattern = pattern;
	}


	public void setProperties(Properties properties) {
		System.out.println("load " + properties.size() + " properties!");
		this.properties = properties;
	}


	public void setTrimString(String trimString) {
		this.trimString = trimString;
	}


	public void setUrl(URL url) {
		System.out.println("setting url:" + url.toExternalForm());
		this.url = url;
	}
	
	
	public static class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {

		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
			registry.registerCustomEditor(Date.class, new CustomDateEditor(dateFormatter,true));
			registry.registerCustomEditor(String.class, new StringTrimmerEditor(true));
		}
		
	}


	public static void main(String[] args) throws IOException {
		File file = File.createTempFile("test", "txt");
		file.deleteOnExit();
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:spring/ch4/property-editor-bean.xml");
		ctx.refresh();
		
		PropertyEditorBean bean = ctx.getBean(PropertyEditorBean.class);
		ctx.close();
	}

}
